package task5;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Game {
    private List<Card> cards;
    private List<Player> players;
    private List<Card> base;

    public Game(){
        cards = new ArrayList<>();
        players = new ArrayList<>();
        base = new ArrayList<>();

        // 初始化全部扑克牌放入cards中
        String[] tmp = "2,A,K,Q,J,10,9,8,7,6,5,4,3".split(",");
        for(int i = 0; i < tmp.length; i++){
            for(int j = 0; j < 4; j++){
                cards.add(new Card(tmp[i], i + 3));
            }
        }
        cards.add(new Card("大王", 1));
        cards.add(new Card("小王", 2));

        // shuffle cards
        Collections.shuffle(cards);

        // 初始化三个player放入players中
        for(int i = 0; i < 3; i++){
            players.add(new Player("player" + (i+ 1)));
        }
    }

    /**
     * 展示全部牌
     */
    public void showAll(){
        System.out.println(cards);
    }

    /**
     * 发牌
     */
    public void deal(){
        int count  = 54;
        Iterator iterator = cards.iterator();
        while(iterator.hasNext() && count > 3){
            Card card = (Card)iterator.next();
            Player player = players.get(count % 3);
            player.dealCards(card);
            players.set(count % 3, player);
            iterator.remove();
            count--;
        }
        base.addAll(cards);
        System.out.println("发牌完成");
    }

    /**
     * 展示玩家手中扑可拍
     */
    public void showPlayerCard(){
        for(Player player: players){
            player.showCards();
        }
    }

    /**
     * 展示底牌
     */
    public void showBaseCard(){
        System.out.println("底牌为：" + base);
    }

    public static void main(String[] args) {
        Game game = new Game();
//        game.showAll();
        game.deal();
        game.showPlayerCard();
        game.showBaseCard();
    }
}
